package com.shanwen.shopping.activity;

import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.classic.core.utils.ToastUtil;
import com.shanwen.shopping.R;
import com.shanwen.shopping.adapter.ConfirmOrderListAdapter;
import com.shanwen.shopping.entity.GoodsInfo;
import com.shanwen.shopping.utils.GoodsDaoUtil;
import com.shanwen.shopping.utils.Utils;
import com.shanwen.shopping.view.MyListView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by lex on 2017/9/29.
 */

public class OrderCommitActivity extends AppBaseActivity {
    @BindView(R.id.etCardNum)
    EditText etCardNum;
    @BindView(R.id.etName)
    EditText etName;
    @BindView(R.id.etPhone)
    EditText etPhone;
    @BindView(R.id.tvValidateName)
    TextView tvValidateName;
    @BindView(R.id.btnValidate)
    Button btnValidate;
    @BindView(R.id.ivProtocol)
    ImageView ivProtocol;
    @BindView(R.id.tvProtocol)
    TextView tvProtocol;
    @BindView(R.id.lv)
    MyListView lv;
    @BindView(R.id.sv)
    ScrollView sv;
    @BindView(R.id.vLineConfirmBottom)
    View vLineConfirmBottom;
    @BindView(R.id.tvConfirmPrice)
    TextView tvConfirmPrice;
    @BindView(R.id.btnConfirm)
    Button btnConfirm;
    @BindView(R.id.llConfirmBottom)
    LinearLayout llConfirmBottom;
    @BindView(R.id.etRemark)
    EditText etRemark;
    private boolean isProtocol = false;
    private ConfirmOrderListAdapter adapter;
    private List<GoodsInfo> list;
    private GoodsDaoUtil goodsDaoUtil;

    @Override
    public int getLayoutResId() {
        return R.layout.activity_order_commit;
    }


    @Override
    public void initView() {
        super.initView();
        setTitle("订单提交");
        setBackEnabled(true);
        tvProtocol.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
        tvProtocol.getPaint().setAntiAlias(true);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
        goodsDaoUtil = new GoodsDaoUtil(this);
        list = new ArrayList<>();
        adapter = new ConfirmOrderListAdapter(this, list);
        lv.setAdapter(adapter);
        list.addAll(goodsDaoUtil.queryAllGoodsInfoByQueryBuilder());
        adapter.notifyDataSetChanged();
        long totalPrice = 0;
        for (int i = 0; i < list.size(); i++) {
            totalPrice += list.get(i).getNum() * list.get(i).getSalePrice();
        }
        tvConfirmPrice.setText(Utils.formatPrice(totalPrice));
    }

    @OnClick({R.id.btnValidate, R.id.ivProtocol, R.id.tvProtocol, R.id.btnConfirm})
    public void click(View view) {
        switch (view.getId()) {
            case R.id.btnValidate:
                String cardNum = etCardNum.getText().toString();
                if (null != cardNum && cardNum.length() != 0) {
                    boolean isNum = cardNum.matches("[0-9]+");
                    if (isNum) {
                        tvValidateName.setText("张*五");
                    } else {
                        ToastUtil.showToast(OrderCommitActivity.this, "请输入正确的卡号");
                    }
                } else {
                    ToastUtil.showToast(OrderCommitActivity.this, "请输入收款人卡号");
                }
                break;
            case R.id.ivProtocol:
                if (isProtocol) {
                    isProtocol = false;
                    ivProtocol.setImageResource(R.drawable.charge_unselected);
                } else {
                    isProtocol = true;
                    ivProtocol.setImageResource(R.drawable.charge_selected);
                }
                break;
            case R.id.tvProtocol:
                WebViewActivity.start(activity, "用户协议", "http://121.41.18.34:600/prisonserver/protocol.html");
                break;
            case R.id.btnConfirm:
                if (null == etCardNum.getText() || etCardNum.getText().toString().length() == 0) {
                    ToastUtil.showToast(OrderCommitActivity.this, "请输入收款人卡号");
                    return;
                }
                if (null == etName.getText() || etName.getText().toString().length() == 0) {
                    ToastUtil.showToast(OrderCommitActivity.this, "请输入您的姓名");
                    return;
                }
                if (null == etPhone.getText() || etPhone.getText().toString().length() == 0) {
                    ToastUtil.showToast(OrderCommitActivity.this, "请输入您的手机号");
                    return;
                }
                if (!isProtocol) {
                    ToastUtil.showToast(OrderCommitActivity.this, "请同意用户协议");
                    return;
                }
                Intent intent = new Intent(OrderCommitActivity.this, OrderPayActivity.class);
                intent.putExtra("price", tvConfirmPrice.getText().toString());
                startActivity(intent);
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        //2分钟后回到主界面
        AppBaseActivity.TIMER_TIME = 2 * 60 * 1000;
    }
}
